import { brands } from './brand'
import { deleteAction, getAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
import { deleteCustomService,move } from '@/api/micromall'
export const listMixin = {
  data() {
    return {
      dataSource: [],
      brands,
      form: this.$form.createForm(this),
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      }
    }
  },
  created() {
    
  },
  mounted(){
    this.loadData()
  },
  methods: {
    async move (record,moveType,type){
      const re = await move({id:record.id,move:moveType,type})
      if(re.code == 200){
        this.$message.success('移动成功')
        this.loadData()
      }else{
        this.$message.error(re.msg||'移动失败')
      }
    },
    deleteService(record,type,title,content){
      this.$confirm({
        title: title||'确定删除吗?',
        content:content||'',
        cancelText: '暂不',
        onOk: res => {
          console.log(res)
          return new Promise((resolve, reject) => {
            deleteCustomService({id:record.id,type})
              .then(res => {
                console.log(res)
                if (res.code == 200) {
                  this.visible = false
                  this.reCalculatePage(1)
                  this.loadData()
                  this.$message.success('删除成功')
                  resolve()
                } else {
                  this.$message.success(res.msg)
                  reject()
                }
              })
              .catch(err => {
                resolve()
                console.log(err)
              })
          }).catch(() => console.log('Oops errors!'))
        },
        onCancel() {}
      })
      // const re = await deleteCustomService({id:record.id,type})
      // if(re.code == 200){
      //   this.$message.success('删除成功')
      //   this.loadData()
      // }else{
      //   this.$message.error(re.msg||'删除失败')
      // }
    },
    handleSubmit(e) {
      e.preventDefault()
      this.pagination.current = 1
      this.loadData()
    },
    resetForm() {
      console.log('重置表达')
      this.form.resetFields()
      this.loadData(1)
    },
    searchBrand(weid) {
      const brand = this.brands.filter(item => item.weid == weid)
      if (brand.length > 0) {
        return brand[0]
      } else {
        return { name: '' }
      }
    },
    getQueryParams() {
      const params = this.form.getFieldsValue()
      const { current, pageSize: size } = this.pagination
      return {
        ...params,
        current,
        size
      }
    },
    handleTableChange(pagination) {
      console.log('gaibian', pagination)
      this.pagination = pagination
      this.loadData()
    },
    reCalculatePage(count){
      //总数量-count
      let total=this.pagination.total-count;
      //获取删除后的分页数
      let currentIndex=Math.ceil(total/this.pagination.pageSize);
      //删除后的分页数<所在当前页
      if(currentIndex<this.pagination.current){
        this.pagination.current=currentIndex;
      }
      console.log('currentIndex',currentIndex)
    },
    deleteRecord(record) {
      if (!this.url.delete) {
        return this.$message.error('请设置url.delete属性!')
      }
      this.$confirm({
        title: '确定删除吗?',
        cancelText: '暂不',
        onOk: res => {
          console.log(res)
          return new Promise((resolve, reject) => {
            deleteAction(this.url.delete,{ id: record.id })
              .then(res => {
                console.log(res)
                if (res.code == 200) {
                  this.visible = false
                  this.reCalculatePage(1)
                  this.loadData()
                  this.$message.success('删除成功')
                  resolve()
                } else {
                  this.$message.success(res.msg)
                  reject()
                }
              })
              .catch(err => {
                resolve()
                console.log(err)
              })
          }).catch(() => console.log('Oops errors!'))
        },
        onCancel() {}
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      getAction(this.url.list, params).then(res => {
        if (res.code == 200) {
          //update-end---author:wsp    Date:20210826
          this.dataSource = res.data.records || res.data
          if (res.data.total) {
            this.pagination.total = res.data.total
          } else {
            this.pagination.total = 0
          }
        } else {
          this.$message.warning(res.msg)
        }
        this.loading = false
      })
    }
  }
}
